package com.itfenghuang.controller;

import com.itfenghuang.bean.User;
import com.itfenghuang.service.imp.UserServiceImp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登陆的Servlet
 */
@SuppressWarnings("all")
public class LoginServlet extends HttpServlet {
    private UserServiceImp service=new UserServiceImp();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        String s = service.selectUserByUsername(user);
        //如果用户输入的账号和密码正确,返回值登陆成功,转发到首页
        //如果用户输入的账号和密码不正确,转发到登陆页面
        if ("登陆成功".equals(s)){
            //转发到成功界面
            HttpSession session = req.getSession();
            session.setAttribute("username",username);
            req.setAttribute("error",s);
            RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
            rd.forward(req,resp);
        }else {
            req.setAttribute("error",s);
            RequestDispatcher rd = req.getRequestDispatcher("login.jsp");
            rd.forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }
}
